class CollectionRatingsController < ApplicationController
  # GET /collection_ratings
  # GET /collection_ratings.json
  def index
    
    if current_user
    
      @collection_ratings = CollectionRating.all
      respond_to do |format|
        format.html # index.html.erb
        format.json { render :json => @collection_ratings }
      end
      
    else
      redirect_to_login
    end
      
  end

  # GET /collection_ratings/1
  # GET /collection_ratings/1.json
  def show
    @collection_rating = CollectionRating.find(params[:id])
    @collection = current_collection

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @collection_rating }
    end
  end

  # GET /collection_ratings/new
  # GET /collection_ratings/new.json
  def new
    @collection_rating = CollectionRating.new
    @collection = Collection.find_by_id(params[:collection_id])
    session[:collection_id] = @collection.id
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @collection_rating }
    end
  end

  # GET /collection_ratings/1/edit
  def edit
    @collection_rating = CollectionRating.find(params[:id])
  end

  # POST /collection_ratings
  # POST /collection_ratings.json
  def create
    @collection_rating = CollectionRating.new(params[:collection_rating])
    @collection_rating.collection_id = current_collection.id

    @collection_rating.user=current_user
    
    respond_to do |format|
      if @collection_rating.save
        format.html { redirect_to @collection_rating, :notice => 'Collection rating was successfully created.' }
        format.json { render :json => @collection_rating, :status => :created, :location => @collection_rating }
      else
        format.html { render :action => "new" }
        format.json { render :json => @collection_rating.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /collection_ratings/1
  # PUT /collection_ratings/1.json
  def update
    @collection_rating = CollectionRating.find(params[:id])

    respond_to do |format|
      if @collection_rating.update_attributes(params[:collection_rating])
        format.html { redirect_to @collection_rating, :notice => 'Collection rating was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @collection_rating.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /collection_ratings/1
  # DELETE /collection_ratings/1.json
  def destroy
    @collection_rating = CollectionRating.find(params[:id])
    @collection_rating.destroy

    respond_to do |format|
      format.html { redirect_to collection_ratings_url }
      format.json { head :no_content }
    end
  end
end
